<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>待接收</title>
    <c:import url="../../header.jsp"/>
    <script> var ALLOT_REFUSED = '${ALLOT_REFUSED}';</script>
</head>
<body>
<div class="body-main">
    <div class="main-title">
				<span class="bus-state">
					<font>待接收</font>
				</span>
        <div class="title-tab">
            <a href="${basePath}/sc/receives/wait_receive.html?status=${ALL}" <c:if test="${status eq 'all'}"> class="active"</c:if>>全部</a>
            <a href="${basePath}/sc/receives/wait_receive.html?status=${SC_TO_RECEIVE_FPDJS}" <c:if test="${status==SC_TO_RECEIVE_FPDJS}"> class="active"</c:if>">分配待接收</a>
            <a href="${basePath}/sc/receives/wait_receive.html?status=${SC_TO_RECEIVE_ZDDJS}" <c:if test="${status==SC_TO_RECEIVE_ZDDJS}"> class="active"</c:if>>转单待接收</a>
             <a href="${basePath}/sc/receives/wait_receive.html?status=${SC_TO_RECEIVE_YJDJS}" <c:if test="${status==SC_TO_RECEIVE_YJDJS}"> class="active"</c:if>>移交待接收</a>
             <a href="${basePath}/sc/receives/wait_receive.html?status=${SC_TO_RECEIVE_TDDJS}" <c:if test="${status==SC_TO_RECEIVE_TDDJS}"> class="active"</c:if>>退回待接收</a>
        </div>
        <input type="hidden" id="statusArr" name="statusArr" value="${status}">
    </div>
    <div class="titleSearch">
        <div class="layui-inline">
            <label>客户电话：</label>
            <div class="layui-input-inline">
                <input name="customerPhone" type="text" class="input-style" maxlength="50"
                       placeholder="请输入客户电话">
            </div>
        </div>
                      <div class="layui-inline">
            <label>订单来源：</label>
            <div class="layui-input-inline">
                      <select class="select-style" id="signOrgId" name="signOrgId">
                            <option value>请选择</option>
                       </select>
            </div>
        </div>
		 <div class="layui-inline">
            <label>来源方式：</label>
            <div class="layui-input-inline">
                      <select class="select-style" id="originCode" name="originCode">
                            <option value>请选择</option>
                      </select>
            </div>
        </div>
        <div class="layui-inline">
            <label>业务类型：</label>
            <div class="layui-input-inline">
                <div id="business_typeCode">
                    <input type="hidden" name="businessType" class="plug-in-msg"/>
                    <input type="hidden" name="typeCode" id="typeCode" class="plug-in-code"/>
                    <input type="text" readonly placeholder="请选择业态" class="chose-plugIn layui-input"
                           name="businessTypeName">
                    <div class="bus-picker">
                        <div class="bus-picker-tab">
                            <a href="javascript:void(0);" class="active">请选择业态</a>
                        </div>
                        <div class="bus-picker-content"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-inline">
            <label>协单人员：</label>
            <div class="layui-input-inline">
                <select  id="assistId" name="assistId" class="assistId">
                    <option value="">--请选择--</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label>签单时间：</label>
            <div class="layui-inline">
                <input type="text" name="signTimeStart" placeholder="开始时间" class="Wdate input-style" id="d4311"
                       onFocus="var d4312=$dp.$('d4312'); WdatePicker({onpicked:function(){d4312.focus();}, maxDate:'#F{ $dp.$D(\'d4312\')}', dateFmt:'yyyy-MM-dd'})">
            </div>
            <span>~</span>
            <div class="layui-inline">
                <input type="text" name="signTimeEnd" placeholder="结束时间" class="Wdate input-style" id="d4312"
                       onFocus="WdatePicker({minDate:'#F{$dp.$D(\'d4311\')}', dateFmt:'yyyy-MM-dd'})">
            </div>
        </div>
        <div class="layui-inline">
            <label>分配时间：</label>
            <div class="layui-inline">
                <input type="text" name="allotTimeStart" placeholder="开始时间" class="Wdate input-style" id="d4313"
                       onFocus="var d4314=$dp.$('d4314'); WdatePicker({onpicked:function(){d4314.focus();}, maxDate:'#F{ $dp.$D(\'d4314\')}', dateFmt:'yyyy-MM-dd'})">
            </div>
            <span>~</span>
            <div class="layui-inline">
                <input type="text" name="allotTimeEnd" placeholder="结束时间" class="Wdate input-style" id="d4314"
                       onFocus="WdatePicker({minDate:'#F{$dp.$D(\'d4313\')}', dateFmt:'yyyy-MM-dd'})">
            </div>
        </div>
        <div class="layui-inline">
            <label>关键字：</label>
            <div class="layui-input-inline">
                <input name="searchKey" type="text" class="input-style" maxlength="50" style="width:250px"
                       placeholder="客户名/订单编号/生产订单编号/合同编号">
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn layui-btn-mini layui-btn-normal" onclick="waitReceiveDataTableReload()">搜索</button>
            <button class="layui-btn layui-btn-mini layui-btn-primary" onclick="cleanSearch()">清空</button>
            <d:hasUrlAuth authCode="ZZSC_WAIT_RECEIVE_EXPORT">
            <button class="layui-btn layui-btn-mini layui-btn-green" onclick="export_data()">导出</button>
            </d:hasUrlAuth>
        </div>
    </div>
    <div class="table-conter">

            <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="callPhoneByProductionPage()">打电话</a>
            <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="receiveOrder()">接收</a>
   		<a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="blackOrderPage()">退回</a>
     <!-- 	<a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="returnOrderPage()">退单</a> -->
        <d:hasUrlAuth authCode="ZZSC_WAIT_RECEIVE_ALLOT">
            <button class="layui-btn layui-btn-mini layui-btn-normal" onclick="openDeptAllotPage()">部门分配</button>
        </d:hasUrlAuth>
 <!--      <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="bindFiles('waitReceiveScOrderTable')">档案绑定</a> -->
        <div class="fr">
            <span class="bgYellow">接收超期</span>
        </div>
    </div>
    <div class="table-body">
        <!--表格 start-->
        <table id="waitReceiveScOrderTable" class="cell-border crm-table columnsHidden dataTable">
            <thead>
            <tr>
                <th width="20"><input type="checkbox" class="checkall"/></th>
                <th width="50">序号</th>
                <th>订单来源</th>
                <th>订单编号</th>
                <th>生产订单编号</th>
                <th>合同编号</th>
                <th>客户</th>
                <th>业务类型</th>
                <th>订单产品</th>
                <th>销售订单备注</th>
                <th>签单时间</th>
                <th>签单商务</th>
                <th>合同金额</th>
                <th>合同周期</th>
                 <th>分配人</th>
                 <th>分配时间</th>
                 <th>协单人员</th>
                 <th>来源方式</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
        <!--表格 end-->
    </div>
</div>
<!-- 退回 -->
    <div class="distribPersonel" style="display:none" id="blackRemarkBox">
    <form action="" id="returnOrderForm" >
        <table class="add-table">
            <tr>
                <th><span class="form-label required">备注：</span></th>
                <td>
                    <div class="layui-input-inline select2-inline">
                         <textarea class="icass-textarea" rows="5" id="return_order_remark" name="return_order_remark" placeholder="请描述备注内容"></textarea>
                    </div>
                </td>
            </tr>
        </table>
        </form>
    </div>

<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script type="text/javascript">
    var NORMAL = '${NORMAL}', TIME_OUT = '${TIME_OUT}';
</script>
<script type="text/javascript" src="${basePath}/static/js/zzsc/production/sc_common.js?v=${version}"></script>
<script type="text/javascript" src="${basePath}/static/js/zzsc/common/call_common.js?v=${version}"></script>
<script type="text/javascript" src="${basePath}/static/js/zzsc/inprocess/inprocess_list.js?v=${version}"></script>
<script src="${rootUrl}/static/js/zzsc/common/sc_chooseUser.js?v=${version}" type="text/javascript" charset="utf-8"></script>
<script src="${rootUrl}/static/js/zzsc/credential/credentailCommon/credentailCommon.js?v=${version}" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
Plug.plug_ins("post", "${basePath}/zzsc/common/tree_book_list.do?code=BUS_YT_ZZ&&type=2&&level=3", "#business_typeCode", '选择业态', '', true, 'BUS_YT_ZZ', '', '');
</script>
<script type="text/javascript">
	var receiveTableObj;
	$(function(){
		//
	syb("#signOrgId");//初始化事业部
	commonTreeBookCode("#originCode","ORDER_SOURCE",1,1,1,true);//初始化订单来源
        //初始化协单人员
        chooseUserPlug.init($('.assistId'), {
            url: sysInfo.basePath + '/zzsc/common/find_user_choose.do',
            otherParam: {
                /**
                 * type: 用户信息选择类型（必填）
                 *       all:全部用户，division：当前登录人所在事业部，department:当前登录人所在部门及以下，role: 查询指定角色用户，
                 * roleCode : 角色Code，（type为role时，必填）
                 * locked: 用户状态，（选填，默认全部状态）
                 *      0、正常；1、锁定；2、离职
                 */
                type: 'role',
                roleCode: 'ZZSC_FLOW_USER',
                locked: 0
            }
        });
});
    // 打电话
    function callPhoneByProductionPage() {
        var dataArr = $('#waitReceiveScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
        	 DGG.Warning('请选择一条订单')
            return false;
        }
        if (dataArr.length > 1) {
        	 DGG.Warning('只能选择一条订单');
            return false;
        }
        callPhoneByProduction(dataArr[0].id);
    }
    // 接收
    function receiveOrder() {
        var dataArr = $('#waitReceiveScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
        	 DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
        	 DGG.Warning('只能选择一条订单');
            return false;
        }
        console.log(dataArr);
     	// DGG.Confire('确认接收后,对应生产订单将进入办理环节','确认是否接收?',function(){
    	//  	$.post(sysInfo.basePath + "/sc/receives/received_order.do",{id:dataArr[0].id},function(result){
    	//  		if(result.code==0){
    	//  			DGG.Succeed('接收成功');
    	//  			$('#waitReceiveScOrderTable').dataTableExtend('reload');
    	//  		}else{
    	//  			DGG.Errors(result.msg);
    	//  		}
    	//  	},'json')
    	// });

            DGG.loading();
            $.ajax({
                type: 'POST',
                async: false,
                url: sysInfo.basePath + '/zzsc/inprocess/validation_update_node.do',
                data:{id:dataArr[0].id},
                dataType: 'JSON',
                success: function (obj) {
                    DGG.loading('close');
                    if (obj.code === 0) {
                        DGG.loading();
                        $.post( sysInfo.basePath+"/zzsc/inprocess/open_update_node.html",{id:dataArr[0].id},function (data) {
                            dgg.Open({
                                title: '更新节点',
                                content: data ,
                                area: '700px',
                                yes:function () {
                                    updateNodeReceiveZz();
                                }
                            });
                        },'html');
                    } else {
                        DGG.Errors(obj.msg);
                    }
                },
                error: function () {
                    DGG.loading('close');
                    DGG.Errors('系统异常,请联系管理员!');
                }
            });
    }
    // 退回
    function blackOrderPage() {
        var dataArr = $('#waitReceiveScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
        	 DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
        	 DGG.Warning('只能选择一条订单');
            return false;
        }
        blackOrderAjaxSubmit(dataArr[0].id);
    }
    // 退单
    function returnOrderPage() {
        var dataArr = $('#waitReceiveScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
        	 DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
        	 DGG.Warning('只能选择一条订单');
            return false;
        }
        chargeBackOrder("waitReceiveScOrderTable",dataArr[0].id);
    }
    // 转单
    function transferOrderPage() {
        var dataArr = $('#waitReceiveScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
        	 DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
        	 DGG.Warning('只能选择一条订单');
            return false;
        }
        transferOrderAjaxSubmit('waitReceiveScOrderTable',dataArr[0].id);
    }
    // 弹出退回页面
    function blackOrderAjaxSubmit(id) {
        DGG.loading();
  		 $("#return_order_remark").val("");
  	     DGG.Open({
	            content:$("#blackRemarkBox"),
	            title:"订单退回",
	            area:'400px',
	            yes:function(index,layero){
	            	 DGG.loading();	
	            	 var return_remark = $("#return_order_remark").val();
	            	 if(return_remark.length>200){
	            		 DGG.Warning("备注内容不能超过200字");
						 DGG.loading("close");	
						return false; 
	            	 }
					var param =$("#returnOrderForm").serializeObject();//获取退回参数
					param.id=id;
					$.post(sysInfo.basePath+"/sc/receives/go_back.do",param,function(result){
						if(result.code==0){
							DGG.Succeed('退回成功');
							receiveTableObj.ajax.reload();
							//$('#waitReceiveScOrderTable').dataTableExtend('reload');
							layer.close(index);
							DGG.loading("close");	
						}else{
							DGG.Errors(result.msg);
							DGG.loading("close");	
						}
					},'json');
					
	            }
	        }) 
    }
    // 弹出退单页面
    function returnOrderAjaxSubmit(id) {
        DGG.loading();
  	     DGG.Open({
	            content:$("#returnOrderPageBox"),
	            title:"订单退单",
	            area:'400px',
	            yes:function(index,layero){
	            	 DGG.loading();	
	            	 var return_remark = $("#return_remark").val();
	            	 if(return_remark.length>200){
	            		 DGG.Warning("备注内容不能超过200字");
						 DGG.loading("close");	
						return false; 
	            	 }
					var param =$("#returnOrderPageBoxForm").serializeObject();//获取退回参数
					param.id=id;
					$.post(sysInfo.basePath+"/sc/distribution/returnOrder.do",param,function(result){
						if(result.code==0){
							DGG.Succeed('退回成功');
							$('#waitReceiveScOrderTable').dataTableExtend('reload');
							layer.close(index);
							DGG.loading("close");	
						}else{
							DGG.Errors(result.msg);
							DGG.loading("close");	
						}
					},'json');
					
	            }
	        }) 
    }
    function export_data() {
        export_table('/sc/receives/export.do', getSearchData());
    }
    
    // 获取搜索数据
    function getSearchData() {
        return $.extend($('.main-title').serializeJson(), $('.titleSearch').serializeJson());
    }

    $(document).ready(function () {
        //初始化dataTable列表
		//初始化dataTable列表
        receiveTableObj = $('#waitReceiveScOrderTable').dataTableExtend({
            "ajax": {"url": "${basePath}/sc/receives/json.do", "type": "post"},
            "fnServerParams": function (aoData) {
                aoData = $.extend(aoData, getSearchData());
                return aoData;
            },
            "scrollX": "true",//是否开启横项滚动
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 3,//左侧锁定2列
                "rightColumns": 1//右侧锁定1列
            },
            "aoColumns": [
                {
                    "orderable": false,
                    "data": "id",
                    "render": function (data, type, full, meta) {
                        return '<input type="checkbox"  name="ids" class="checkchild"  value="' + data + '" />';
                    }
                }
                , {
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return meta.row + 1;
                    }
                }, {
                    "orderable": false,
                    "data": "signOrgName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }, {
                    "orderable": false,
                    "data": "orderNo",
                    "render": function (data, type, full, meta) {
                    	 return ('<a href=\'javascript:void(0)\' ti  onclick="openBusinessDetail(\'' + full.orderNo + '\',\'' + full.orderId + '\')" /><span class="layui-table-link">' + data + '</span></a>&nbsp;');
                    }
                }, {
                    "orderable": false,
                    "data": "scProductOrderNo",
                    "render": function (data, type, full, meta) {
                        var result ="";
                        result += '<a href="javascript:;"  class="layui-table-link" onclick="openOrderInfoDetail(\'' + full.scProductOrderNo + '\',\'' + full.id + '\')" /><p>' + data + '</p></a>';
                        var reservationStatus = full.reservationStatus;
                        if (reservationStatus){
                            var split = reservationStatus.split(',');
                            for (var i = 0; i < split.length;i++){
                                var splitElement = split[i];
                                if ('PRODUCT_RESERVATION_STATUS_01' === splitElement){//预约
                                    result += '<a href="javascript:;"   class="label label-yellow" onclick="certificateAppointment(\'' + full.id + '\')">'+'已预约'+'</a>';
                                }else if ('PRODUCT_RESERVATION_STATUS_02' === splitElement){//预定
                                    result += '<a href="javascript:;"   class="label label-green" onclick="openCertificateMatch(\'' + full.id + '\')">'+'已预定'+'</a>';
                                }
                            }
                        }
                        return result;
                    }
                },
                {
                    "orderable": false,
                    "data": "contractNo",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "customerName",
                    "render": function (data, type, full, meta) {
                   	  return emptyStringConvert(full.customerName) + '<p>' + emptyStringConvert(full.customerPhone) + '</p>';
                    }
                },
                {
                    "orderable": false,
                    "data": "businessProductName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "productName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "orderRemark",
                    "render": function (data, type, full, meta) {
                        if(data){
                            return  '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ';
                        }else{
                            return '';
                        }
                    }
                },
                {
                    "orderable": true,
                    "data": "signTime",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "signUserName",
                    "render": function (data, type, full, meta) {
                        return data+'<p>'+full.signUserOrgName+'</p>';
                    }
                },{
                    "orderable": false,
                    "data": "money"
                },
                {
                    "orderable": false,
                    "data": "contractStartDate",
                    "render": function (data, type, full, meta) {
                        return data ? data.replace(new RegExp("-",'g'),"")+"-"+full.contractEndDate.replace(new RegExp("-",'g'),"") : '';
                    }
                },
                {
                    "orderable": false,
                    "data": "allotUserName",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },{
                    "orderable": true,
                    "data": "allotDate",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }, { //协单人员
                    "sClass": "hoverProcess",
                    "orderable": false,
                    "data": "assistUserName",
                    "render": function (d, type, r) {
                        var names = '';
                        if (d) {
                            var split = d.split(",");
                            names = '<span>' + split[0] + '</span>';
                            if (split.length > 1) {
                                names += '<i>...</i>'
                            }
                        }
                        return names;
                    }
                },{
                    "orderable": false,
                    "data": "originCode",
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }],
            "columnDefs": [{
                targets: [3], //第1，2，3列
                createdCell: function (td, cellData, rowData, row, col) { 
                  	if(null!=rowData.receiveTimeStatus){//只有分配待接收超期才显示黄色
                	  	if(rowData.nodeTimeStatus=='${TIME_OUT}' && rowData.status=='ZZSC_STATE_SCPROORDER_CODE2'){
                	 		 $(td).parent().addClass("bgYellow");
                	 		 return;
                	 	}
                	}
                }
            }]
        });
    });
	///搜索
	function waitReceiveDataTableReload(){
		receiveTableObj.ajax.reload();
	}
    //协单人员展示相关
    $(document).on('mouseenter', '.hoverProcess', function () {
        var datas = receiveTableObj.cell(this).data();
        if (datas == undefined) {
            return false;
        }
        datas = datas.split(",");
        var _top = $(this).offset().top + $(this).height() + 'px',
            _left = $(this).offset().left + 'px';
        if (datas != undefined && datas.length > 1) {
            var htm = '<div class="process-relative" id="processRel" style="top:' + _top + ';left:' + _left + '">'
            for (var i in datas) {
                htm += '<p class="line2">' + datas[i] + '</p>';
            }
            htm += '</div>'
        }
        $('#processRel').remove();
        $('body').append(htm)
    });
    $(document).on('mouseleave', '#processRel', function () {
        $('#processRel').remove();
    });
    $(document).on('click', function (e) {
        var case1 = $(e.target).parents('.hoverProcess').length,
            case2 = $(e.target).hasClass('.hoverProcess'),
            case3 = $(e.target).parents('#processRel').length;
        if (case3 != 1) {
            $('#processRel').remove();
        }
    });

    // 分配
    function openDeptAllotPage() {
        var dataArr = $('#waitReceiveScOrderTable').dataTableExtend('getCheck');
        if (dataArr.length == 0) {
            DGG.Warning('请选择一条订单');
            return false;
        }
        if (dataArr.length > 1) {
            DGG.Warning('只能选择一条订单');
            return false;
        }
        openAllotScOrderPage(dataArr[0].id);
    }

    // 弹出分配页面
    function openAllotScOrderPage(id) {
        DGG.loading();
        $.post(sysInfo.basePath + "/sc/distribution/deptAllotScOrderPage.html", {id: id}, function (html) {
            DGG.Open({
                content: html,
                title: "部门分配",
                area: '500px',
                success:function(layero,index){
                    $('.layerIndex2').val(index);
                },
                yes: function (index, layero) {
                    DGG.loading();
                    var allotWay = $("#allotWay").val();//获取分配方式
                    var flowUserId = $("#flowUserId").val();//获取分配人员
                    if (null == allotWay || '' == allotWay) {
                        DGG.Warning("请选择分配方式");
                        DGG.loading("close");
                        return false;
                    }
                    if (null == flowUserId || '' == flowUserId) {
                        DGG.Warning("请选择分配人员");
                        DGG.loading("close");
                        return false;
                    }
                    var param = {};
                    param.receiverId = flowUserId;
                    param.allotWay = allotWay;
                    param.id = id;
                    //协单人员
                    param.assistIds = getVal('#assistIdPop', 0);
                    param.lastRemark = $("[name='lastRemark']").val();
                    param.isNeedDispose = $("[name='isNeedDispose']:checked").val();
                    param.type = 1;//待接收部门分单标识
                    $.post(sysInfo.basePath + "/sc/distribution/allotScOrder.do", param, function (result) {
                        if (result.code == 0) {
                            DGG.Succeed('分配成功');
                            receiveTableObj.ajax.reload();
                            layer.close(index);
                            DGG.loading("close");
                        } else {
                            DGG.Errors(result.msg);
                            DGG.loading("close");
                        }
                    }, 'json');

                }
            })

        }, 'html');
    }
</script>
</body>

</html>